//
//  RecommendViewController.m
//  Zyachting
//
//  Created by NGUYEN THE HUNG on 11/6/12.
//
//

#import "RecommendViewController.h"
#import "TextViewCell.h"
#import "EnterDataCell.h"
@interface RecommendViewController ()

@end

@implementation RecommendViewController
@synthesize isInquired;
@synthesize scroller;
@synthesize tableView;
@synthesize infoDict;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.isInquired) {
        self.navigationItem.title = @"Your inquiry";
    }else{
        self.navigationItem.title = @"Recommend";
    }
    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    if (self.isInquired) {
        [scrollView setContentSize:CGSizeMake(320, 450)];
    }else{
        [scrollView setContentSize:CGSizeMake(320, 400)];
    }

    [scrollView setDelegate:self];
    [scrollView setBackgroundColor:[UIColor clearColor]];
    [scrollView setDelegate:self];
    [self.view addSubview:scrollView];


    
    //TableView for displaying property, change height if needed
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 367) style:UITableViewStyleGrouped] autorelease];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setScrollEnabled:NO];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setBackgroundView:nil];
    [scrollView addSubview:self.tableView];
    
    UIButton* inquireBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [inquireBtn setImage:[UIImage imageNamed:@"SendBtn.png"] forState:UIControlStateNormal];
    if (self.isInquired) {
        [inquireBtn setFrame:CGRectMake(15, 380, 290, 45)];
    }else{
        [inquireBtn setFrame:CGRectMake(15, 336, 290, 45)];
    }

    [inquireBtn addTarget:self action:@selector(sendBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [inquireBtn setTag:0];
    [scrollView addSubview:inquireBtn];
    
    self.scroller = [scrollView autorelease];
}
-(IBAction)sendBtnTapped:(id)sender
{
    MFMailComposeViewController* mailComposer = [[MFMailComposeViewController alloc] init];
    [mailComposer setMailComposeDelegate:self];
    if (self.isInquired) {
        [mailComposer setToRecipients:@[tf3.text]];
    }else{
        [mailComposer setToRecipients:@[tf2.text]];
    }
    [mailComposer setMessageBody:tv.text isHTML:NO];
    [[self appDelegate].tabBarController presentViewController:mailComposer animated:YES completion:NULL];
    [mailComposer release];
}
-(AppDelegate*)appDelegate
{
    return (AppDelegate*)([UIApplication sharedApplication].delegate);
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark TableView DataSource Method
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        if (self.isInquired) {
            return 3;
        }else{
            return 2;
        }
    }
    return 1;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *EnterDataCellIdentifier = @"EnterDataCellIdentifier";
        EnterDataCell *cell = (EnterDataCell *)[tableView1
                                                  dequeueReusableCellWithIdentifier: EnterDataCellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EnterDataCell"
                                                         owner:self options:nil];
            for (id oneObject in nib)
                if ([oneObject isKindOfClass:[EnterDataCell class]])
                    cell = (EnterDataCell *)oneObject;
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (self.isInquired) {
            switch (indexPath.row) {
                case 0:
                    cell.tfEnterData.placeholder = @"Name";
                    tf1 = cell.tfEnterData;
                    break;
                case 1:
                    cell.tfEnterData.placeholder = @"Phone";
                    tf2 = cell.tfEnterData;
                    break;
                case 2:
                    cell.tfEnterData.placeholder = @"Email";
                    tf3 = cell.tfEnterData;
                    break;
                default:
                    break;
            }
        }else{
            switch (indexPath.row) {
                case 0:
                    cell.tfEnterData.placeholder = @"Your name";
                    tf1 = cell.tfEnterData;
                    break;
                case 1:
                    cell.tfEnterData.placeholder = @"Email of the recipient";
                    tf2 = cell.tfEnterData;
                    break;

                default:
                    break;
            }
        }
        [tf1 setDelegate:self];
        [tf2 setDelegate:self];
        [tf3 setDelegate:self];
        return cell;
    }else if(indexPath.section == 1){
        static NSString *TextViewCellIdentifier = @"TextViewCellIdentifier";
        TextViewCell *cell = (TextViewCell *)[tableView1
                                                  dequeueReusableCellWithIdentifier: TextViewCellIdentifier];
        if (cell == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TextViewCell"
                                                         owner:self options:nil];
            for (id oneObject in nib)
                if ([oneObject isKindOfClass:[TextViewCell class]])
                    cell = (TextViewCell *)oneObject;
        }
        tv = cell.tvContent;
        tv.text = [NSString stringWithFormat:@"http://www.z-yachting.com/en/search-on-map?view=property&id=%d",[self.infoDict[@"id"] integerValue]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    return nil;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (self.isInquired) {
        if (section == 0) {
            
            return @"Your data";
        }else if(section == 1){
            return @"Comments or questions";
        }
        return nil;
    }else{
        if (section == 0) {
            
            return @"Your name, email of the recipient";
        }else if(section == 1){
            return @"Text of the recommendation";
        }
        return nil;
    }

    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 44;
    }else if(indexPath.section == 1)
    {
        return 120;
    }
    return 0;
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
    [controller dismissViewControllerAnimated:YES completion:NULL];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == tf1) {
        [tf1 resignFirstResponder];
        [tf2 becomeFirstResponder];
    }else if(textField == tf2){
        [tf2 resignFirstResponder];
        [tf3 becomeFirstResponder];
    }else if(textField == tf3){
        [tf3 resignFirstResponder];
        [tv becomeFirstResponder];
        [tv setReturnKeyType:UIReturnKeyDefault];
    }
    return YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [tf1 resignFirstResponder];
    [tf2 resignFirstResponder];
    [tf3 resignFirstResponder];
    [tv resignFirstResponder];
}
@end
